package com.team.employment.web.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.employment.model.entity.CompanyInfo;
import com.team.employment.model.entity.JobApplication;
import com.team.employment.model.entity.RecruitmentInfo;
import com.team.employment.model.enums.JobApplicationType;
import com.team.employment.web.admin.mapper.CompanyInfoMapper;
import com.team.employment.web.admin.mapper.RecruitmentInfoMapper;
import com.team.employment.web.admin.service.CompanyInfoService;
import com.team.employment.web.admin.vo.SubmitResumeInfoVo;
import com.team.employment.web.app.service.JobApplicationService;
import com.team.employment.web.app.mapper.JobApplicationMapper;
import com.team.employment.web.app.vo.ApplicationResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
* @author 陈钰儿
* @description 针对表【job_application】的数据库操作Service实现
* @createDate 2025-04-27 21:02:57
*/
@Service
public class JobApplicationServiceImpl extends ServiceImpl<JobApplicationMapper, JobApplication>
    implements JobApplicationService{

    @Autowired
    private JobApplicationMapper jobApplicationMapper;

    @Autowired
    private RecruitmentInfoMapper recruitmentInfoMapper;

    @Autowired
    private CompanyInfoMapper companyInfoMapper;



    @Override
    public IPage<ApplicationResultVo> getUserJobApplication(Long id, long current, long size) {
        LambdaQueryWrapper<JobApplication> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(JobApplication::getUserId,id);
        List<JobApplication> jobApplicationList = jobApplicationMapper.selectList(queryWrapper);

        ApplicationResultVo vo = new ApplicationResultVo();
        List<ApplicationResultVo> voList = new ArrayList<>();
        jobApplicationList.forEach(jobApplication -> {
            BeanUtils.copyProperties(jobApplication,vo);

            //得到招聘信息
            LambdaQueryWrapper<RecruitmentInfo> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(RecruitmentInfo::getId,jobApplication.getRecruitmentId());
            RecruitmentInfo recruitmentInfo = recruitmentInfoMapper.selectOne(queryWrapper1);

            vo.setRecruitmentInfo(recruitmentInfo);

            //得到公司名字
            LambdaQueryWrapper<CompanyInfo> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(CompanyInfo::getId,recruitmentInfo.getCompanyId());
            CompanyInfo companyInfo = companyInfoMapper.selectOne(queryWrapper2);

            vo.setCompanyName(companyInfo.getName());

            voList.add(vo);
        });
        IPage<ApplicationResultVo> voIPage = new Page<>(current,size);
        voIPage.setRecords(voList);


        return voIPage;
    }

}




